<?php
/*
 WayToShip base class for shipping modules
 for more details visit waytoship.com
 */

require_once(DIR_FS_CATALOG . DIR_WS_CLASSES . "way2ship_osc.php");

class w2scarrier {
	var $code, 
		$title,
		$display_title, 
		$description, 
		$ICON_CONST,
		$STATUS_CONST,
		$ZONE_CONST,
		$SORT_ORDER_CONST,
		$HANDLING_FEE_CONST,
		$CARRIER_PREFIX_CONST,
		$FALLBACK_SERVICE_CONST,
		$FALLBACK_SERVICE_COST_CONST,
		$carrier_status,
		$carrier_zone,
		$carrier_code,
		$handling_fee,
		$carrier_name,
		$fallback_service,
		$fallback_service_name,
		$fallback_service_cost,
		$tax_class, 		
		$enabled;

	// class constructor
	function w2scarrier() {
		global $order;
		$this->description = $this->description . ' [' . W2S_MODULE_VERSION . ']';
		$this->enabled = (($this->carrier_status == 'True') ? true : false);
		$this->icon = DIR_WS_ICONS . $this->ICON_CONST;

		if ( ($this->enabled == true) && ((int)$this->carrier_zone > 0) ) {
			$check_flag = false;
			$check_query = tep_db_query("select zone_id from " . TABLE_ZONES_TO_GEO_ZONES . " where geo_zone_id = '" . $this->carrier_zone . "' and zone_country_id = '" . $order->delivery['country']['id'] . "' order by zone_id");
			while ($check = tep_db_fetch_array($check_query)) {
				if ($check['zone_id'] < 1) {
					$check_flag = true;
					break;
				} elseif ($check['zone_id'] == $order->delivery['zone_id']) {
					$check_flag = true;
					break;
				}
			}

			if ($check_flag == false) {
				$this->enabled = false;
			}
		}
		$this->tax_class = SHIPPING_WAY2SHIP_TAX_CLASS;
		$settings = new Way2ShipSettings();
		$services = $settings->getServicesByCarrier($this->carrier_code);
		$this->fallback_service = array_search($this->fallback_service_name, $services);
	}

	// class methods
	function quote($method = '') {
		global $order, $cart;

		$w2s = new Way2ShipOSC();
		$methods = $w2s->quote($order, $cart, $this->carrier_code, $this->get_services_offered(), $this->handling_fee,
			$this->fallback_service, $this->fallback_service_cost);
		if (count($methods) == 0) {
			// no services available for this quote
			return;
		}

		if ($method != '') {
			$selected_method = array();
			for ($i=0; $i < count($methods); $i++) {
				if ($methods[$i]['id'] == $method) {
					$selected_method[] = $methods[$i];
					break;
				}
			}
			$methods = $selected_method;
		}

		$this->quotes = array('id' => $this->code, 'module' => $this->display_title);

		if (is_string($methods)) {
			// error returned. should be array otherwise
			$this->quotes['error'] = $methods;
			return $this->quotes;
		}
		 
		$this->quotes['methods'] = $methods;
		if (tep_not_null($this->icon)) $this->quotes['icon'] = tep_image($this->icon, $this->display_title);


		if ($this->tax_class > 0) {
			$this->quotes['tax'] = tep_get_tax_rate($this->tax_class, $order->delivery['country']['id'], $order->delivery['zone_id']);
		}

		return $this->quotes;
	}

	function check() {
		if (!isset($this->_check)) {
			$check_query = tep_db_query("select configuration_value from " . TABLE_CONFIGURATION . " where configuration_key = '$this->STATUS_CONST'");
			$this->_check = tep_db_num_rows($check_query);
		}
		return $this->_check;
	}

	function install() {
		 
		tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, set_function, date_added) values ('Enable " . $this->carrier_name . " WayToShip Shipping', '$this->STATUS_CONST', 'True', 'Do you want to offer " . $this->carrier_name . " shipping through EShipper?', '6', '0', 'tep_cfg_select_option(array(\'True\', \'False\'), ', now())");
		tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, use_function, set_function, date_added) values ('Shipping Zone', '$this->ZONE_CONST', '0', 'If a zone is selected, only enable this shipping method for that zone.', '6', '1', 'tep_get_zone_class_title', 'tep_cfg_pull_down_zone_classes(', now())");
		tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) values ('Sort order of display.', '$this->SORT_ORDER_CONST', '8', 'Sort order of display. Lowest is displayed first.', '6', '2', now())");
		tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) values ('Carrier specific handling fee.', '$this->HANDLING_FEE_CONST', '', 'Apply this handling fee on " . $this->carrier_name . " quotes. Leave empty to use the global handling fee.', '6', '3', now())");

		$settings = new Way2ShipSettings();
		$w2s_carrier_services = $settings->getServicesByCarrier($this->carrier_code);
		
		// populate the services options
		$index = 10;
		foreach ($w2s_carrier_services as $service_id => $service_name) {
			tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, set_function, date_added) values ('Offer " . $service_name . " service', '$this->CARRIER_PREFIX_CONST" . $service_id .
      	"', 'True', 'Do you want to offer " . $service_name . " service?', '6', '$index', 'tep_cfg_select_option(array(\'True\', \'False\'), ', now())");
  			$index++;						
		}

		// the full Services list by Carrier cannot be stored in "configuration.set_function" column because the column is limited to 255 characters 
		// way2ship_install_getServiceByCarrier function (in way2ship_osc.php) allows to avoid this restriction
		$services_function = "tep_cfg_select_option(way2ship_install_getServiceByCarrier($this->carrier_code)";
		
		tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, set_function, date_added) values ('Fallback service', '$this->FALLBACK_SERVICE_CONST', 'None', 'The service to present to a customer when quotes are not available. Leave None during setup to see all error messages.', '6', '20', '$services_function, ', now())");
		tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) values ('Cost of the fallback service.', '$this->FALLBACK_SERVICE_COST_CONST', '', 'Cost of the fallback service defined above.', '6', '21', now())");
		
	}

	function remove() {
		tep_db_query("delete from " . TABLE_CONFIGURATION . " where configuration_key in ('" . implode("', '", $this->keys()) . "')");
	}

	function keys() {
		$settings = new Way2ShipSettings();
		$w2s_services = $settings->getServicesByCarrier($this->carrier_code);
		 
		$keys = array($this->STATUS_CONST, $this->ZONE_CONST, $this->SORT_ORDER_CONST, $this->HANDLING_FEE_CONST);
		foreach ($w2s_services as $service_id => $service_name) {
			$keys[] = $this->CARRIER_PREFIX_CONST . $service_id;
		}
		$keys[] = $this->FALLBACK_SERVICE_CONST;
		$keys[] = $this->FALLBACK_SERVICE_COST_CONST;
		return $keys;
	}

	function get_services_offered() {
		$settings = new Way2ShipSettings();
		$w2s_services = $settings->getServicesByCarrier($this->carrier_code);
		 
		$services_offered = array();
		foreach ($w2s_services as $service_id => $service_name) {
			if (constant($this->CARRIER_PREFIX_CONST . $service_id) == 'True') {
				$services_offered[] = $service_id;
			}
		}
		return $services_offered;
	}
}
?>
